package com.ohs.ohsoa.system.service;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.ohs.ohsoa.system.dto.DepartmentDto;
import com.ohs.ohsoa.system.entity.Department;
import com.ohs.ohsoa.system.mapper.DepartmentMapper;
import com.ohs.ohsoa.system.vo.DepartmentVo;
import com.ohs.ohsoa.utils.IsNull;
import com.ohs.ohsoa.utils.bean.requestBean.PageParam;
import com.ohs.ohsoa.utils.exception.BizException;
import org.springframework.beans.BeanUtils;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 部门实现类
 *
 * @author ss
 *
 */
@Service
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department> implements DepartmentService {


    @Resource
    private DepartmentMapper departmentMapper;

    @Resource
    private MessageSource messageSource;


    @Override
    public List<DepartmentVo> departmentSelectList(PageParam<DepartmentDto> pageParam) {
        DepartmentDto departmentDto = pageParam.getSearchbean();
        // 分页
        PageHelper.startPage(pageParam.getIndexPage(), pageParam.getPageSize());
        return  departmentMapper.departmentSelectList(departmentDto);
    }

    @Override
    public DepartmentVo departmentSelect(DepartmentDto departmentDto) {
        Department department = this.getById(departmentDto.getDepartGuid());
        DepartmentVo departmentVo = new DepartmentVo();
        BeanUtils.copyProperties(department,departmentVo);
        return departmentVo;
    }

    @Override
    public Boolean departmentInsert(DepartmentDto departmentDto) {
        // 是否存在相同的部门名称
        boolean flg = noSameDepartName(departmentDto.getDepartName(), "0",departmentDto.getGroupEnterpriseGuid());
        if (flg) {
            Department department = new Department();
            BeanUtils.copyProperties(departmentDto,department);
            return this.save(department);
        }else {
            throw new BizException("E0003", messageSource.getMessage("E0003",new String[] {"部門名称","部門名称"}, null)) ;
        }
    }

    @Override
    public Boolean departmentDelete(DepartmentDto departmentDto) {
        return departmentMapper.departmentDelete(departmentDto.getDepartGuids());
    }

    @Override
    public Boolean departmentUpdate(DepartmentDto departmentDto) {
        // 是否存在相同的部门名称
        boolean flg = noSameDepartName(departmentDto.getDepartName(), departmentDto.getDepartGuid(),departmentDto.getGroupEnterpriseGuid());
        if(flg){
            Department department = new Department();
            BeanUtils.copyProperties(departmentDto,department);

            return this.updateById(department);
        }else{
            throw new BizException("E0003", messageSource.getMessage("E0003",new String[] {"部門名称","部門名称"}, null)) ;
        }
    }

    @Override
    public List<Department> departmentSelectListByEnterprise(DepartmentDto departmentDto) {
        QueryWrapper<Department> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(IsNull.isNotNull(departmentDto.getGroupEnterpriseGuid()),"group_enterprise_guid",departmentDto.getGroupEnterpriseGuid())
                .eq(IsNull.isNotNull(departmentDto.getIsKaifa()),"is_kaifa",departmentDto.getIsKaifa()).orderByAsc("sort_flag");
        return this.list(queryWrapper);
    }


    /**是否存在相同的部门名称
     *
     * @param departName 部门名称
     * @param departGuid 部门id
     * @param groupEnterpriseGuid 企业id
     * @return 是否存在
     */
    private boolean noSameDepartName(String departName, String departGuid, String groupEnterpriseGuid) {
        // 定义返回值
        boolean flg = false;

        QueryWrapper<Department> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("depart_name",departName)
                .eq("group_enterprise_guid",groupEnterpriseGuid)
                .ne("depart_guid",departGuid)
                .eq("delete_flag","1");
        if(this.count(queryWrapper) == 0){
            flg = true;
        }
        return flg;
    }


}
